/************************************************************************************/
/*  Program name CAPFAIMP_JNL_ENTRIES.sql                                           */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 19 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_JNL_ENTRIES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_JOURNAL_ENTRIES;
	
	lv_Next_Seq_ID		NUMBER;

BEGIN
	UPDATE
		FA_JOURNAL_ENTRIES
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_JOURNAL_ENTRIES
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FA_JOURNAL_ENTRIES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_FA_JOURNAL_ENTRIES_MAP	VALUES
			(
			C1.CJE_ID,   
			lv_Next_Seq_ID 
			);
			
			INSERT INTO FA_JOURNAL_ENTRIES
			VALUES
			(
			lv_Next_Seq_ID,
			CAPBRIMP_MAPPING.GetSOBIDMapping(C1.SET_OF_BOOKS_ID),
			C1.BOOK_TYPE_CODE,
			C1.PERIOD_COUNTER,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.ADDITION_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.ADJUSTMENT_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.DEPRECIATION_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.RECLASS_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.RETIREMENT_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.REVAL_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.TRANSFER_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_ADDITION_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_ADJUSTMENT_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_RECLASS_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_RETIREMENT_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_REVAL_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.CIP_TRANSFER_BATCH_ID),
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.DEPRN_ADJUSTMENT_BATCH_ID),
			C1.JE_STATUS,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.LAST_UPDATE_DATE
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/